﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyQQ
{
    public partial class Frm_Login : Form
    {
        public Frm_Login()
        {
            InitializeComponent();
        }
        DataOperator dataOper = new DataOperator();

        public bool ValidateInput()
        {
            if (txtId.Text.Trim() == "")  //登录账号
            {
                MessageBox.Show("请输入登录账号", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtId.Focus();
                return false;
            }
            else if (int.Parse(txtId.Text.Trim()) > 65535) 
            {
                MessageBox.Show("请输入正确的登录账号", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtId.Focus();
                return false;
            }  
            else if (txtId.Text.Length>5 && txtPwd.Text.Trim()=="") 
            {
                MessageBox.Show("请输入密码", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPwd.Focus();
                return false;
            }
            return true; 
        
        }
        private void Frm_Login_Load(object sender, EventArgs e)
        {

        }

        private void txtId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || e.KeyChar == '\r' || e.KeyChar == '\b')
            {
                e.Handled = false;
            }
            else
                e.Handled = true;
        }

        private void pboxLogin_Click(object sender, EventArgs e)
        {
            if (ValidateInput())
            {
                string sql = "select count(*) from tb_User where ID=" + int.Parse(txtId.Text.Trim())
                    + "and Pwd='" + txtPwd.Text.Trim() + "'";
                int num = dataOper.ExecSQL(sql);
                if (num == 1)
                {
                    PublicClass.loginID = int.Parse(txtId.Text.Trim());
                    if (cboxRemember.Checked)
                    {
                        dataOper.ExecSQLResult("update tb_User set Remember=1 where ID=" + int.Parse(txtId.Text.Trim()));
                        if (cboxLogin.Checked)
                            dataOper.ExecSQLResult("update tb_User set AutoLogin=1 where ID=" + int.Parse(txtId.Text.Trim()));
                    }
                    else
                    {
                        dataOper.ExecSQLResult("update tb_User set Remember=0 where ID=" + int.Parse(txtId.Text.Trim()));
                        dataOper.ExecSQLResult("update tb_User set AutoLogin=0 where ID=" + int.Parse(txtId.Text.Trim()));
                    }
                    dataOper.ExecSQLResult("update tb_User set Flag=1 where ID=" + int.Parse(txtId.Text.Trim()));//设置在线状态
                    Frm_Main frmMain = new Frm_Main();
                    frmMain.Show();
                    this.Visible = false;
                }
                else
                {
                    MessageBox.Show("输入的用户名或密码有误！", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                }
            }

        private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                pboxLogin_Click(sender, e);
            }
        }

        private void linklblReg_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Frm_Main frmmain = new Frm_Main();
            frmmain.Show();
        }

        private void pboxMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void pboxClose_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }
    }
}

